﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.Customer.Record;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Configuration;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class UpdateDistrictServiceCommand : ProviderQueueCommand<IProviderFactory, DistrictRecord, ResponseRecord>
    {
        public UpdateDistrictServiceCommand(IProviderFactory provider) : base(provider) { }
        protected override void Validate(DistrictRecord record)
        {
            if (record == null) throw new ArgumentNullException("request");

            if (!record.DistrictIdentifier.IsNullOrWhiteSpace() && !DistrictIdentifier.IsValid(record.DistrictIdentifier))
            {
                throw new ArgumentException("Identifier is invalidate", "request");
            }

            if (!record.ProvinceIdentifier.IsNullOrWhiteSpace() && !ProvinceIdentifier.IsValid(record.ProvinceIdentifier))
            {
                throw new ArgumentException("ProvinceIdentifier is invalidate", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var district = Record;
            var obj = new District()
            {
                Id = new DistrictIdentifier(district.DistrictIdentifier).PersistableID,
                Name = district.DistrictName,
                EntryDateTime = DateTime.Now,
                //EntryUserId = CONST_NAME.SYNCHRONIZE_ACCOUNT,
                EntryUserName = CONST_NAME.SYNCHRONIZE_ACCOUNT,
                Lat = district.LAT,
                Lng = district.LNG,
                ProvinceId = new ProvinceIdentifier(district.ProvinceIdentifier).PersistableID,
                UpdateDateTime = DateTime.Now,
                //UpdateUserId = CONST_NAME.SYNCHRONIZE_ACCOUNT,
                UpdateUserName = CONST_NAME.SYNCHRONIZE_ACCOUNT
            };
            var itemCheck = Providers.District.RetrieveDistrict(Record.DistrictIdentifier);
            if (itemCheck == null)
            {
                var resultitem1 = Providers.District.Add(obj);
            }
            else
            {
                var resultitem2 = Providers.District.Update(obj);
            }
            return true;
        }
    }
}
